<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //Promise.race()
        const delay = ms => {
            return new Promise(resolve => {
                setTimeout(resolve, ms)
            })
        }

        const p1 = delay(1000).then(() => {
            console.log('p1完成了');
            //  return 'p1'
            return Promise.reject('reason')
        })

        const p2 = delay(2000).then(() => {
            console.log('p2完成了');
            return 'p2'
                // return Promise.reject('reason')
        })


        //Promise.race()的状态取决于第一个完成Promise实例的对象
        //如果第一个完成的成功了，那最终的就成功了，如果第一个完成的失败了 那最终的就失败了

        const racePromise = Promise.race([p1, p2])
        racePromise.then(data => {
            console.log(data);
        }, err => {
            console.log(err);
        })
    </script>
</body>

</html>